

public class CornerOrientationTable{
	public int[][] cOrientTable = new int[8][2186];
	int cOrientCycle(int orient, int typeCycle){ //Rules for the changing of corner orientation.
		/*
		    UFL : 0 -> 1; 1 -> 2; 2 -> 0; is case 0
			UFR : 0 -> 2; 1 -> 0; 2 -> 1; is case 1
			
			NOTE: They are inverses of each other.
			
			all corner orientation transformations take place in one of these two cycles.
		 */
		if (typeCycle == 0){
			orient++;
			orient %= 3;
		}
		else if (typeCycle == 1){
			if (orient > 0) orient--;
			else orient = 2;
		}
		return orient;
	}
	String convert(String ternInitPos, int index, int cycle){ //prob works
		Character a = ternInitPos.charAt(index);
		String c = a.toString();
		int intA = Integer.parseInt(c);
	
		String first = ternInitPos.substring(0, index);
		String second = ternInitPos.substring(index+1); //cuts up the string
		
		String mid = Integer.toString(cOrientCycle(intA, cycle)); //replaces desired index with fitting cycle
		String result = first + mid + second;
//		System.out.printf("Index: %d Initial String:                   %s Cycle: %s \n", index, ternInitPos, cycle);
//		System.out.printf("First: %s Mid: %s Second: %s Result: %s \n", first, mid, second, result);
		return result;
		/*
		 * Index: 4 Initial String:                   0202 2 212 Cycle: 0 
			First: 0202 Mid: 0 Second: 212 Result: 0202 0 212 
		 */
		
	}
	public String getTernary(int decimal){ // gets ternary of a decimal, always 7 digits
		String proto = Integer.toString(decimal,3);
		while (proto.length()<8){
			proto = "0" + proto;
		}
		return proto;
	}
	public int getDecimal(String ternary){ //gets decimal of a ternary string
		
		return Integer.parseInt(ternary, 3);
		 
	}
	
	int getCOResult(int turn, int initPos){
//		UFL : 0 -> 1; 1 -> 2; 2 -> 0; is case 0
//		UFR : 0 -> 2; 1 -> 0; 2 -> 1; is case 1
		
		String ternInitPos = getTernary(initPos);
		switch (turn){ //TODO:'??
		case 0:
		case 4:// positive and negative direction are the same.
			ternInitPos = convert(ternInitPos, 2, 1);
			ternInitPos = convert(ternInitPos, 3, 0);
			ternInitPos = convert(ternInitPos, 6, 0);
			ternInitPos = convert(ternInitPos, 7, 1);
			break;
		case 1:
		case 5:// probably.
			ternInitPos = convert(ternInitPos, 3, 1);
			ternInitPos = convert(ternInitPos, 7, 0);
			ternInitPos = convert(ternInitPos, 5, 1);
			ternInitPos = convert(ternInitPos, 1, 0);
			break;
		case 2:
		case 6:
			ternInitPos = convert(ternInitPos, 0, 0);
			ternInitPos = convert(ternInitPos, 1, 1);
			ternInitPos = convert(ternInitPos, 5, 0);
			ternInitPos = convert(ternInitPos, 4, 1);
			break;
		case 3:
		case 7:
			ternInitPos = convert(ternInitPos, 2, 0);
			ternInitPos = convert(ternInitPos, 6, 1);
			ternInitPos = convert(ternInitPos, 4, 0);
			ternInitPos = convert(ternInitPos, 0, 1);
			break;
			
		}
		return getDecimal(ternInitPos);
		
	}
	
	int posit = 0;
	int genCornerOrientationTable(){
		for (int i = 0; i < 8; i++){
			for (int j = 0; j < 2186; j++){
				cOrientTable[i][j] = getCOResult(i,j); //generate the table
			}
			
		}
		return 0;
	}
	public CornerOrientationTable(){
		//empty constructor
	}
	
}
